
function myAxios(config) {
  const { url, method, params, data } = config;


  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();

    if (method.toUpperCase() === 'GET') {
      const str = setParams(params);
      xhr.open('GET', params ? url + '?' + str : url);
      xhr.send();


    } else {
      xhr.open('POST', url);
      let flag=Object.prototype.toString.call(data)==='[object Object]'

      xhr.setRequestHeader('Content-Type', flag ? 'application/json':'application/x-www-form-urlencoded');

      xhr.send(flag ? JSON.stringify(data):data);
    }
    
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status >= 200 && xhr.status < 300) {
          console.log(1);
          resolve(JSON.parse(xhr.responseText));
        }
        else {
            console.log(2);
            reject(xhr.status);
          }
      } 
    };
  });
}


function setParams(obj) {
  const newArr = [];
  for (let key in obj) {
    let str = key + '=' + obj[key];
    newArr.push(str);
  }
  console.log(newArr);
  return newArr.join('&');
}
